﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FinancialManagement.Models.Repository;
using Telerik.Web.Mvc;
using FinancialManagement.Utils;
using FinancialManagement.Models;

namespace FinancialManagement.Controllers
{
    public class RevenueController : Controller
    {
        private AccountRepository accountRepository = new AccountRepository();
        private RevenueRepository revenueRepository = new RevenueRepository();
        private ComboBoxRepository repository = new ComboBoxRepository();
        //
        // GET: /Revenue/

        public ActionResult Index()
        {
            var isFinancial = accountRepository.GetRoleAdminByUserID(User.Identity.Name);
            ViewData["admin"] = isFinancial;

            ViewData["UserName"] = accountRepository.GetUserNameByUserID(User.Identity.Name);

            ViewBag.DepName = accountRepository.GetDepNameByUserID(User.Identity.Name);
            ViewBag.RevenueYear = Utils.Util.GetAnnualYear(Util.RevenueType.Actual);
            ViewBag.BudgetYear = Utils.Util.GetAnnualYear(Util.RevenueType.Actual) -1;


            var canActual = (bool)Session["canActual"];
            if (canActual)
            {
                return View();
            }
            else
            {
                return View("Error");
            }
        }

        /// <summary>
        /// 显示添加收入预算明细信息的Window
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShowAddWindow(int type)
        {
            ViewBag.StrLen = "(200字以内)";
            ViewBag.WindowTitle = "添加收入预算明细";
            BudgetApplyTable budget = new BudgetApplyTable();
            var username = User.Identity.Name;
            budget.ApplyDepartment = accountRepository.GetDepIdByUserID(username);
            budget.ApplyDepartmentName = accountRepository.GetDepNameByUserID(username);

            if (type == 1)
            {
                ViewData["TYPE"] = "Budget";
                budget.RevenueType = false;
            }
            else
            {
                ViewData["TYPE"] = "Revenue";
                budget.RevenueType = true;
            }

            return View(budget);
        }

        /// <summary>
        /// 显示修改收入预算明细信息的Window
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShowEditWindow(int applyId, int type)
        {
            ViewBag.StrLen = "(200字以内)";
            ViewBag.WindowTitle = "更新收入预算明细";
            BudgetApplyTable budget = revenueRepository.GetBudgetRevenueByApplyId(applyId);

            if (type == 1)
            {
                ViewData["TYPE"] = "Budget";
                budget.RevenueType = false;
            }
            else
            {
                ViewData["TYPE"] = "Revenue";
                budget.RevenueType = true;
            }
            return View(budget);
        }

        /// <summary>
        /// 插入收入预算明细信息
        /// </summary>
        /// <returns></returns>
        public string InsertBudgetApplyTable(BudgetApplyTable budget)
        {
            var model = new BudgetApplyTable();
            if (TryUpdateModel(model))
            {

                model.Illustration = UploadController.FilePath;
                if (model.RevenueType)
                {
                    model.Annual = Util.GetAnnualYear(Util.RevenueType.Actual) - 1;
                }
                else
                {
                    model.Annual = Util.GetAnnualYear(Util.RevenueType.Actual);
                }

                try
                {
                    model.CalculateUnitName = repository.GetCalculateUniteNameByCode(model.CalculateUnitCode);
                    model.ChargeScopeName = repository.GetChargeScopeNameByCode(model.ChargeScopeCode);
                    model.RevenueTypeName = repository.GetRevenueTypeNameByCode(model.RevenueTypeCode);
                }
                catch (Exception)
                {
                    return Util.ERROR;
                }

                //The model is valid - insert the product.                
                var isOk = revenueRepository.InsertBudgetApplyTable(model);
                if (isOk)
                {
                    return Util.SUCCESS;
                }
                return Util.ERROR;
            }
            return Util.ERROR;
        }

        /// <summary>
        /// 更新收入预算明细信息
        /// </summary>
        /// <param name="budget"></param>
        /// <returns></returns>
        public string SaveBudgetApplyTable(BudgetApplyTable budget)
        {
            var model = revenueRepository.GetBudgetRevenueByApplyId(budget.ApplyID);
            TryUpdateModel(model);
            try
            {
                model.CalculateUnitName = repository.GetCalculateUniteNameByCode(model.CalculateUnitCode);
                model.ChargeScopeName = repository.GetChargeScopeNameByCode(model.ChargeScopeCode);
                model.RevenueTypeName = repository.GetRevenueTypeNameByCode(model.RevenueTypeCode);
            }
            catch (Exception)
            {
                return Util.ERROR;
            }
            var isOk = revenueRepository.UpdateBudgetApplyTable(model);
            if (isOk)
            {
                return Util.SUCCESS;
            }
            return Util.ERROR;
        }

        /// <summary>
        /// 删除收入预算明细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public string DeleteBudgetApplyTable(int applyId)
        {
            var isOk = revenueRepository.DeleteBudgetApplyTableByApplyId(applyId);
            if (isOk)
            {
                return Util.SUCCESS;
            }
            return Util.ERROR;
        }

        /// <summary>
        /// 加载id=Budget的Grid
        /// </summary>
        /// <returns></returns>
        [GridAction]
        public ActionResult BindingBudgetRevenue()
        {
            var depId = accountRepository.GetDepIdByUserID(User.Identity.Name);
            var models = revenueRepository.GetBudgetRevenueByDepID(depId, Util.GetAnnualYear(Util.RevenueType.Actual));
            return View(new GridModel(models));
        }

        /// <summary>
        /// 加载id=Actual的Grid
        /// </summary>
        /// <returns></returns>
        [GridAction]
        public ActionResult BindingActualRevenue()
        {
            var depId = accountRepository.GetDepIdByUserID(User.Identity.Name);
            var models = revenueRepository.GetBudgetRevenueByDepID(depId, Util.GetAnnualYear(Util.RevenueType.Actual) - 1);
            return View(new GridModel(models));
        }
    }
}
